package alfabeta;

/**
 * This class implements the logic behind the BDD for the n-queens problem
 * 
 * @author Jacob Brandt Jensen, jaje@itu.dk
 *
 */

public class QueensLogicABP {
    private int x = 0;
    private int y = 0;
    private int[][] board;
    private NQueenBDD queenBdd;

    public QueensLogicABP() {
       //constructor
    }

    public void initializeGame(int size) {
        this.x = size;
        this.y = size;
        this.board = new int[x][y];
        this.queenBdd = new NQueenBDD(size);
    }

   
    public int[][] getGameBoard() {
        return board;
    }

    public boolean insertQueen(int column, int row) {

        if (board[column][row] == -1 || board[column][row] == 1) {
        	return true;       
        }
        
        // Add Queen to board
        board[column][row] = 1;
        
        // Restrict nQueen BDD with the queen added
        this.queenBdd.addQueen(column+1, row+1);
        
        // Find valid domains and update board   
        // iterate through all variables
        for (int i = 0; i<x ;i ++) {
  
        	for (int j=0; j<y; j++) {
  
        		int index = (i*x) + j;
        		
        		// if BDD is unsat when a queen is added at this position - add 0 to board
        		if (this.queenBdd.isValid(index) && board[j][i]!=1) {
        			this.queenBdd.setFalse(index);
        			board[j][i] = -1;
        		}
        		
        	}
        	      	
        }
        
        return true;
    }
    
}
